using System;
using System.Security.Cryptography;
using System.Text;

namespace demo
{
    /// <summary>
    /// 密码加密解密工具类
    /// </summary>
    public static class PasswordEncryption
    {
        // 固定的密钥和IV，用于AES加密
        private static readonly byte[] Key = Encoding.UTF8.GetBytes("1234567890123456"); // 16字节密钥
        private static readonly byte[] IV = Encoding.UTF8.GetBytes("abcdefghijklmnop"); // 16字节IV

        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="plainText">明文密码</param>
        /// <returns>加密后的密码</returns>
        public static string Encrypt(string plainText)
        {
            if (string.IsNullOrEmpty(plainText))
                return string.Empty;

            try
            {
                using (Aes aes = Aes.Create())
                {
                    aes.Key = Key;
                    aes.IV = IV;
                    aes.Mode = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;

                    using (ICryptoTransform encryptor = aes.CreateEncryptor())
                    {
                        byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                        byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
                        return Convert.ToBase64String(encryptedBytes);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"密码加密失败: {ex.Message}", ex);
            }
        }

        /// <summary>
        /// 解密密码
        /// </summary>
        /// <param name="encryptedText">加密后的密码</param>
        /// <returns>明文密码</returns>
        public static string Decrypt(string encryptedText)
        {
            if (string.IsNullOrEmpty(encryptedText))
                return string.Empty;

            try
            {
                using (Aes aes = Aes.Create())
                {
                    aes.Key = Key;
                    aes.IV = IV;
                    aes.Mode = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;

                    using (ICryptoTransform decryptor = aes.CreateDecryptor())
                    {
                        byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
                        byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
                        return Encoding.UTF8.GetString(decryptedBytes);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"密码解密失败: {ex.Message}", ex);
            }
        }

        /// <summary>
        /// 验证加密解密是否正常工作
        /// </summary>
        /// <param name="testPassword">测试密码</param>
        /// <returns>是否验证成功</returns>
        public static bool ValidateEncryption(string testPassword = "test123")
        {
            try
            {
                string encrypted = Encrypt(testPassword);
                string decrypted = Decrypt(encrypted);
                return testPassword == decrypted;
            }
            catch
            {
                return false;
            }
        }
    }
}